/**
 * 
 */
package com.itinsys.enma.manager.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.ObjectExistsException;

import org.springframework.beans.factory.annotation.Autowired;

import com.itinsys.enma.dao.PriorityEnvironmentDAO;
import com.itinsys.enma.domain.PriorityEnvironment;
import com.itinsys.enma.domain.dto.PriorityEnvironmentDTO;
import com.itinsys.enma.domain.response.EnvironmentResponse;
import com.itinsys.enma.domain.response.PriorityEnvironmentResponse;
import com.itinsys.enma.manager.EnvironmentDTOManager;
import com.itinsys.enma.manager.PriorityEnvironmentManager;
import com.itinsys.enma.manager.ResponseManager;
import com.itinsys.enma.util.InternalMessageService;

/**
 * @author pferraz
 *
 */
public class PriorityEnvironmentManagerImpl implements PriorityEnvironmentManager{

	@Autowired
	private PriorityEnvironmentDAO priorityEnvironmentDAO;
	
	@Autowired
	ResponseManager responseManager;
	
	@Autowired
	private InternalMessageService messageService;
	
	@Autowired
	private EnvironmentDTOManager environmentsDTOManager;
	
	@Override
	public PriorityEnvironmentResponse addPriorityEnvironment(PriorityEnvironment priorityEnvironment) {
		boolean exists=false;
		try{
			priorityEnvironmentDAO.getByEnvironmentIdAndShiftId(priorityEnvironment.getEnvironment().getId(),priorityEnvironment.getShift().getId());
			exists=true;
		}catch(ObjectExistsException e){}
		
		PriorityEnvironmentResponse response= new PriorityEnvironmentResponse();;
		if(!exists){
			priorityEnvironmentDAO.addPriorityEnvironment(priorityEnvironment);
			 response = getAllPriorityEnvironment();
		}else{
			 responseManager.prepareResponse(response, PriorityEnvironmentResponse.PRIORITY_ENVIRONMENT_EXISTS);
		}
		return response;
	}


	@Override
	public PriorityEnvironmentResponse deletePriorityEnvironment(PriorityEnvironment priorityEnvironment) {
		PriorityEnvironmentResponse response=new PriorityEnvironmentResponse();
		priorityEnvironment=priorityEnvironmentDAO.getById(priorityEnvironment.getEnvironment().getId(),priorityEnvironment.getShift().getId(),priorityEnvironment.getSchool().getId());
		
		if(priorityEnvironmentDAO.deletePriorityEnvironment(priorityEnvironment))		
			response=getAllPriorityEnvironment();
		else
			responseManager.prepareResponse(response, PriorityEnvironmentResponse.ERROR);
		
		return response;
	}

	@Override
	public PriorityEnvironmentResponse getAllPriorityEnvironment() {
		PriorityEnvironmentResponse response = new PriorityEnvironmentResponse();
		response.setPriorityEnvironmentDTOs(priorityEnvironmentToDTO(priorityEnvironmentDAO.getAllPriorityEnvironment()));
		processResponse(response);

		return response;
	}

	private List<PriorityEnvironmentDTO> priorityEnvironmentToDTO(List<PriorityEnvironment> priorityEnvironment) {
		List<PriorityEnvironmentDTO> priorityEnvironmentDTOs=new ArrayList<PriorityEnvironmentDTO>();
		PriorityEnvironmentDTO priorityEnvDTO;
		for(PriorityEnvironment pE:priorityEnvironment){
			priorityEnvDTO=new PriorityEnvironmentDTO(environmentsDTOManager.envToDTO(pE.getEnvironment()), pE.getShift(), pE.getSchool());
			priorityEnvironmentDTOs.add(priorityEnvDTO);
		}
		return priorityEnvironmentDTOs;
	}

	private void processResponse(PriorityEnvironmentResponse pEnvResp) {
	
		responseManager.prepareResponse(pEnvResp, EnvironmentResponse.OK);
		
		pEnvResp.setMessage(messageService.getMessage(pEnvResp.getMessagesCode().get(pEnvResp.getResponseCode())));
	}
	
	
}
